//
//  ReleaseTextAndImageController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/17.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "ReleaseTextAndImageController.h"

@interface ReleaseTextAndImageController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**文字输入框*/
@property (nonatomic,strong)PubulicTextView*textView;
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;

@end

@implementation ReleaseTextAndImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavItems];
    [self createUI];
}
-(void)releaseAction{
    ShowActivityHud(@"发布中...");
//    NSMutableArray * images = [NSMutableArray array];//图片地址数组
//    for (NSInteger i = 0 ; i < self.dataArray.count ; i++) {
//        PhotoModel * model = self.dataArray[i];
//        NSString * name = [ImageAndVideoSaveTool get_ONLYID];
//        [images addObject:name];
//        [ImageAndVideoSaveTool saveImageDocuments:model.highDefinitionImage imageName:name];
//    }
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:@"购物狂" forKey:@"name"];
    [dic setValue:@"user2" forKey:@"userIcon"];
    [dic setValue:self.textView.textView.text forKey:@"title"];
//    [dic setValue:images forKey:@"images"];
//    [PlistTool savePlistValue:dic Type:ReleseImageAndText];
    kWeakSelf(weakSelf);
    [PlistTool savePlistValue:dic Type:ReleseImageAndText Images:self.dataArray successful:^(NSString * _Nonnull message) {
        DLogFunc;
        HideHubView;
        [weakSelf postNotification];
        [weakSelf backBtnClicked];
    }];
}
-(void)postNotification{
    [[NSNotificationCenter defaultCenter] postNotificationName:K_ReleaseTextAndImage object:nil];
}
-(void)setNavItems{
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    leftBtn.titleLabel.font = SYSTEMFONT(16);
    [leftBtn sizeToFit];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"发布" forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
    rightBtn.titleLabel.font = SYSTEMFONT(16);
    [rightBtn sizeToFit];

    kWeakSelf(weakSelf);
    [rightBtn addActionHandler:^{
        if (self.textView.textView.text.length == 0) {
            ShowHubMessage(@"请添加分享的内容");
            return ;
        }
        if (self.dataArray.count == 0) {
            ShowHubMessage(@"请添加分享的图片");
            return ;
        }
        [weakSelf releaseAction];
    }];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];

    
}
-(void)createUI{
    [self.view addSubview:self.textView];
    [self.view addSubview:self.listView];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(90);
    }];
}
#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count + 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ReleaseImageItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(ReleaseImageItem.class) forIndexPath:indexPath];
    if (indexPath.row == self.dataArray.count) {
        cell.isAdd = YES;
    }else{
        cell.model = self.dataArray[indexPath.row];
        cell.isAdd = NO;
        kWeakSelf(weakSelf);
        cell.deleBlck = ^{
            DLogFunc;
            [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
            [weakSelf.listView reloadData];
        };
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.dataArray.count) {
        kWeakSelf(weakSelf);
        [PhotosManager showPhotosManager:self withMaxImageCount:9 withAlbumArray:^(NSMutableArray<PhotoModel *> * _Nonnull albumArray) {
            DLog(@"%@", albumArray);
            for (PhotoModel * model in albumArray) {
                if (model.highDefinitionImage != nil) {
                    [weakSelf.dataArray addObject:model];
                }
            }
//            [weakSelf.dataArray addObjectsFromArray:albumArray];
            [weakSelf.listView reloadData];
        } withCameraImage:^(UIImage * _Nonnull cameraImage) {
            PhotoModel * model = [[PhotoModel alloc] init];
            model.highDefinitionImage = cameraImage;
            [weakSelf.dataArray addObject:model];
            [weakSelf.listView reloadData];
        }];

    }
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (KScreenWidth - 42)/3;
    return CGSizeMake(width, width);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

#pragma mark ---懒加载
-(PubulicTextView*)textView{
    if(!_textView){
        _textView=[PubulicTextView new];
        _textView.placeholderText.text = @"分享新鲜事";
        _textView.placeholderText.textColor = UIColorFromRGB(0x959E97);
        _textView.placeholderText.font = SYSTEMFONT(14);
        _textView.backgroundColor = KRedColor;
        _textView.textView.font = BOLDSYSTEMFONT(18);
        _textView.textView.textColor=UIColorFromRGB(0x273323);
    }
    return _textView;
}
-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        _listView = [[UICollectionView alloc] initWithFrame:CGRectMake(16, 140, KScreenWidth-32, KScreenHeight-K_TopHeight-36-140) collectionViewLayout:self.layout];
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = YES;
        [_listView registerClass:[ReleaseImageItem class] forCellWithReuseIdentifier:NSStringFromClass(ReleaseImageItem.class)];
    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end

@interface ReleaseImageItem ()
/**图片*/
@property (nonatomic,strong)UIImageView*imageView;
/**添加图标*/
@property (nonatomic,strong)UIImageView*addImage;
@end

@implementation ReleaseImageItem

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorFromRGB(0xF5F5F5);
        [self createUI];
    }
    return self;
}
-(void)setIsAdd:(BOOL)isAdd{
    _isAdd = isAdd;
    self.imageView.hidden = isAdd;
    self.addImage.hidden = !isAdd;
}
-(void)setModel:(PhotoModel *)model{
    _model = model;
    self.imageView.image = model.highDefinitionImage;
}
-(void)createUI{
    UIButton * deleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleBtn setBackgroundImage:IMAGE_NAMED(@"icon_quxiao") forState:UIControlStateNormal];
    kWeakSelf(weakSelf);
    [deleBtn addActionHandler:^{
        BlockSafeRun(weakSelf.deleBlck);
    }];
    
    [self addSubview:self.imageView];
    [self addSubview:self.addImage];
    
    [self.imageView addSubview:deleBtn];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.addImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    
    [deleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-4);
        make.top.mas_equalTo(4);
    }];
}
#pragma mark ---懒加载
-(UIImageView*)imageView{
    if(!_imageView){
        _imageView=[UIImageView new];
        _imageView.backgroundColor = KBlueColor;
        _imageView.userInteractionEnabled = YES;
    }
    return _imageView;
}
-(UIImageView*)addImage{
    if(!_addImage){
        _addImage=[[UIImageView alloc] initWithImage:IMAGE_NAMED(@"add_image")];
    }
    return _addImage;
}

@end
